# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.13
pkgrel=1
pkgdesc="General purpose library and file format for storing scientific data"
arch=('any')
url="http://www.hdfgroup.org/HDF5/"
source=("http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-${pkgver}/src/hdf5-${pkgver}.tar.bz2"
        "hdf5-link-ws2_32-mingw.patch"
        "hdf5-default-import-suffix.patch"
        "hdf5-install.patch"
        "hdf5-fix-install-docs.patch"
        )
md5sums=('b060bb137d6bd8accf8f0c4c59d2746d'
         '35e0198f4ed456ee5673c23a06aa5eb6'
         'f6150aad270fe6f2dc6d6402926e67e9'
         'f08826ac66e319190a01d3f82a692c0e'
         '44ac9317cb413eca5ed506033fd3b676')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran" "${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-szip")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-gcc-fortran" "${MINGW_PACKAGE_PREFIX}-cmake" "patch")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/hdf5-link-ws2_32-mingw.patch
  patch -p1 -i ${srcdir}/hdf5-install.patch
  patch -p1 -i ${srcdir}/hdf5-default-import-suffix.patch
  patch -p1 -i ${srcdir}/hdf5-fix-install-docs.patch
  
  # rename X.dll to X-0.dll
  echo "IF (BUILD_SHARED_LIBS)" >> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_HL_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_CPP_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_HL_CPP_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_TOOLS_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_F90_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_F90_C_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_C_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  echo "ENDIF ()" >> CMakeLists.txt
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DHDF5_BUILD_HL_LIB=ON \
    -DHDF5_BUILD_CPP_LIB=ON \
    -DHDF5_BUILD_FORTRAN=ON \
    -DHDF5_BUILD_TOOLS=ON \
    -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
    -DHDF5_ENABLE_SZIP_SUPPORT=ON \
    -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 install
}
